/**
 * Created by guo.xiaomin on 2017-12-03.
 */
var async = require('async');
var moment = require('moment');
var messageUtil = require('../common/utils/messageUtil');
var toolUtil = require('../common/utils/toolUtil');

exports.getCommissionH5Flag = function (callback) {
    var commissionH5FlagRedisKey = "h5:commission:commissionH5FlagRedisKey";
    redisClient.get(commissionH5FlagRedisKey, function (err, commissionH5FlagValue) {
        if (commissionH5FlagValue) {
            logger.debug("commissionH5FlagValue从redis中取得值..." + commissionH5FlagValue);
            callback(null, commissionH5FlagValue);
        } else {
            callback(null, null);
        }
    });
};

exports.setCommissionH5Flag = function (setValue, callback) {
    var commissionH5FlagRedisKey = "h5:commission:commissionH5FlagRedisKey";
    redisClient.set(commissionH5FlagRedisKey, setValue, function (err, commissionH5FlagValue) {
        callback(null, "OK");
    });
};

exports.getCommissionH5UserFlag = function (callback) {
    var commissionH5UserFlagRedisKey = "h5:commission:commissionH5UserFlagReidsKey";
    redisClient.get(commissionH5UserFlagRedisKey, function (err, commissionH5UserFlagValue) {
        if (commissionH5UserFlagValue) {
            logger.debug("commissionH5FlagValue从redis中取得值..." + commissionH5UserFlagValue);
            callback(null, commissionH5UserFlagValue);
        } else {
            callback(null, null);
        }
    });
};

exports.setCommissionH5UserFlag = function (setValue, callback) {
    var commissionH5UserFlagRedisKey = "h5:commission:commissionH5UserFlagReidsKey";
    redisClient.set(commissionH5UserFlagRedisKey, setValue, function (err, commissionH5UserFlagValue) {
        callback(null, "OK");
    });
};

exports.isAllowCommissionH5User = function (userId, callback) {

    var flag = true;
    try{
        var id = mongoose.Types.ObjectId(userId);
    } catch (e) {
        flag = false;
        logger.error(userId + '不是合法的mongodb的id');
        callback(e, null);
    }
    if(flag) {
        var redisKey = "h5:commission:allowCommissionH5UserIdsReidsKey";
        redisClient.get(redisKey, function (err, returnValue) {
            logger.debug("白名单人员..." + returnValue);
            if (returnValue && returnValue.indexOf(userId) != -1) {
                callback(null, true);
            } else {
                callback(null, false);
            }
        });
    }
};

exports.addAllowCommissionH5User = function (userId, callback) {
    var flag = true;
    try{
        var id = mongoose.Types.ObjectId(userId);
    } catch (e) {
        flag = false;
        logger.error(userId + '不是合法的mongodb的id');
        callback(e, null);
    }
    if(flag) {
        var redisKey = "h5:commission:allowCommissionH5UserIdsReidsKey";
        redisClient.get(redisKey, function (err, returnValue) {
            logger.debug("白名单人员..." + returnValue);
            if (returnValue) {
                // 原来就有
                if (returnValue.indexOf(userId) != -1) {
                    callback(null, "noChange");
                } else {
                    // 原来没有，但是有别人
                    var tempUserIdArray = returnValue.split(',');
                    tempUserIdArray.push(userId);
                    var newUserIds = tempUserIdArray.toString();
                    redisClient.set(redisKey, newUserIds, function (err, value) {
                        callback(null, "add");
                    });
                }
            } else {
                redisClient.set(redisKey, userId, function (err, value) {
                    callback(null, "set");
                });
            }
        });
    }
};

exports.removeAllowCommissionH5User = function (userId, callback) {

    var flag = true;
    try{
        var id = mongoose.Types.ObjectId(userId);
    } catch (e) {
        flag = false;
        logger.error(userId + '不是合法的mongodb的id');
        callback(e, null);
    }
    if(flag) {
        var redisKey = "h5:commission:allowCommissionH5UserIdsReidsKey";
        redisClient.get(redisKey, function (err, returnValue) {
            logger.debug("白名单人员..." + returnValue);
            if (returnValue) {
                // 原来有
                if (returnValue.indexOf(userId) != -1) {
                    // 原来没有，但是有别人
                    var tempUserIdArray = returnValue.split(',');
                    var newArray = [];
                    async.eachSeries(tempUserIdArray, function (temp, callbackInner) {
                        if (temp != userId) {
                            newArray.push(temp);
                        }
                        callbackInner(false);
                    }, function (err) {
                        var newUserIds = newArray.toString();
                        redisClient.set(redisKey, newUserIds, function (err, value) {
                            callback(null, "remove");
                        });
                    });
                } else {
                    callback(null, "noChange");
                }
            } else {
                callback(null, "notExists");
            }
        });
    }
};

exports.getAllAllowCommissionH5User = function (callback) {
    var redisKey = "h5:commission:allowCommissionH5UserIdsReidsKey";
    redisClient.get(redisKey, function (err, returnValue) {
        logger.debug("白名单人员..." + returnValue);
        if (returnValue) {
            callback(null, returnValue);
        } else {
            callback(null, null);
        }
    });
};

exports.clearAllowCommissionH5User = function (callback) {
    var redisKey = "h5:commission:allowCommissionH5UserIdsReidsKey";
    redisClient.set(redisKey, "", function (err, returnValue) {
        callback(null, "OK");
    });
};

exports.isAllowCommissionH5UserList = function (userIdList, callback) {
    var flag = true;
    if(flag) {
        exports.getExitsNotExitsArray(userIdList, function (err, results) {
            callback(err, results);
        });
    }
};

exports.addAllowCommissionH5UserList = function (userIdList, callback) {
    var flag = true;
    if(flag) {
        var redisKey = "h5:commission:allowCommissionH5UserIdsReidsKey";
        exports.getExitsNotExitsArray(userIdList, function (err, results) {
            var exitsArray = obj.exitsArray;
            var notExitsArray = obj.notExitsArray;
            if(exitsArray || exitsArray.length > 0) {
                // 有存在的
                callback("exitsArray", null);
            }
            if(notExitsArray || notExitsArray.length > 0) {
                var addValue = notExitsArray.toString();
                redisClient.get(redisKey, function (err, returnValue) {
                    logger.debug("白名单人员..." + returnValue);
                    var tempUserIdArray = returnValue.split(',');
                    tempUserIdArray.push(addValue);
                    var newUserIds = tempUserIdArray.toString();
                    redisClient.set(redisKey, newUserIds, function (err, value) {
                        callback(null, "add");
                    });
                });
            }
        });
    }
};

exports.removeAllowCommissionH5UserList = function (userIdList, callback) {
    callback(null, null);
};


exports.getExitsNotExitsArray = function (userIdList, callback) {
    var tempUserIdArray = userIdList.split(',');
    var redisKey = "h5:commission:allowCommissionH5UserIdsReidsKey";
    redisClient.get(redisKey, function (err, returnValue) {
        logger.debug("白名单人员..." + returnValue);
        if (returnValue) {
            var exitsArray = [];
            var notExitsArray = [];
            async.eachSeries(tempUserIdArray, function (temp, callbackInner) {
                if (returnValue.indexOf(temp) != -1) {
                    exitsArray.push(temp);
                    callbackInner(false);
                } else {
                    notExitsArray.push(temp);
                    callbackInner(false);
                }
            }, function (err) {
                var obj = {};
                obj.exitsArray = exitsArray;
                obj.notExitsArray = notExitsArray;
                callback(null, obj);
            });
        } else {
            var obj = {};
            obj.exitsArray = [];
            obj.notExitsArray = tempUserIdArray;
            callback(null, obj);
        }
    });
};

exports.getSmsVerificationCode = function (mobilePhone, type, callback) {
    var key = 'ver:code:' + mobilePhone + ':' + type;
    redisClient.get(key, function (err, value) {
        logger.debug("短信验证码..." + key + "@" + value);
        callback(null, value);
    });
};

exports.getRedisValueByKey = function (redisKey, callback) {
    redisClient.get(redisKey, function (err, value) {
        logger.debug("根据redis的key取得redis值..." + redisKey + "@" + value);
        callback(null, value);
    });
};